﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Collections.Concurrent;

namespace WeChatGZHTextExtractor
{
    public enum LogType
    {
        Info,
        Debug,
        Error,
    }

    public class LogHelper
    {
        private static LogHelper _LogHelper = new LogHelper();

        private ConcurrentQueue<string> Messages = new ConcurrentQueue<string>();
        private AutoResetEvent ResetEvent = new AutoResetEvent(false);

        private LogHelper()
        {
            new Thread(() =>
            {
                while (true)
                {
                    if (Messages.TryDequeue(out string message))
                        Console.WriteLine(message);
                    else
                        ResetEvent.WaitOne(1000);
                }
            })
            {
                IsBackground = true
            }.Start();
        }

        public static LogHelper GetInstance()
        {
            return _LogHelper;
        }

        public void Log(LogType type, params string[] messages)
        {
            Messages.Enqueue($"[{type}]\t{string.Join("\r\n\t", messages ?? new string[0])}");
            ResetEvent.Set();
        }
    }
}
